<template>
  <div>
    <div class="table_div">
      <!-- 搜索 -->
      <el-form ref="formSearch" :model="params" :inline="true" size="small">
        <el-form-item label="应用系统" prop="appId">
          <el-select v-model="params.appId" placeholder="请选择菜单应用系统" @change="toQuery">
            <el-option
              v-for="item in appLists"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="菜单名称" prop="name">
          <el-input
            v-model="params.name"
            clearable
            class="filter-item"
            placeholder="支持模糊查询"
          />
        </el-form-item>
        <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="toQuery">搜索</el-button>
        <el-button class="filter-item" size="mini" icon="el-icon-close" @click="resetSearchData">重 置</el-button>
        <el-button
          class="filter-item"
          size="mini"
          type="warning"
          icon="el-icon-more"
          @click="changeExpand">{{ expand ? '折叠' : '展开' }}</el-button>
      </el-form>
    </div>
    <div class="table_div">
      <el-button type="primary" size="mini" icon="el-icon-plus" @click="add">新 增</el-button>
    </div>
    <!--表单组件-->
    <eForm ref="form" :is-add="isAdd"/>
    <!--表格渲染-->
    <el-table
      v-loading="loading"
      :data="data"
      :height="tableHeight"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      :default-expand-all="expand"
      row-key="name"
      border
      stripe
      highlight-current-row
      size="small"
    >
      <el-table-column prop="name" label="菜单名称" width="200px" align="center"/>
      <el-table-column prop="icon" label="菜单图标" width="125px" align="center"/>
      <el-table-column prop="type" label="菜单类型" width="150px" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type === 0" size="small">目录</el-tag>
          <el-tag v-else-if="scope.row.type === 1" size="small" type="success">菜单</el-tag>
          <el-tag v-else-if="scope.row.type === 2" size="small" type="info">按钮</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="url" label="菜单Url" width="150px" align="center"/>
      <el-table-column prop="perms" label="授权标识" width="150px" align="center"/>
      <el-table-column prop="orderNum" label="排序" width="150px" align="center"/>
      <el-table-column label="操作" width="125" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit" @click="edit(scope.row)"/>
          <el-popover
            :ref="scope.row.id"
            placement="top"
            width="180">
            <p>确定删除本条数据吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
              <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定</el-button>
            </div>
            <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini"/>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
/* eslint-disable */
import initData from '@/mixins/initData'
import initDict from '@/mixins/initDict'
import {getDepts,del } from '@/api/menu'
import {listAll } from '@/api/app'
import eForm from './form'
import Config from '@/config'

export default {
  components: { eForm },
  mixins: [initData,initDict],
  data() {
    return {
      delLoading: false,
      expand: false,
      appLists:[],
      params:{
        appId:'1'
      },
    }
  },
  created() {
    this.setHeight(250); //高度需要调整
    this.$nextTick(() => {
      this.getAppList()
      this.initAllData()
    })
  },
  methods: {
    beforeInit() {
      this.url = 'menu/getMenuByAppId'
      return true
    },
    changeExpand() {
      this.expand = !this.expand;
      this.initAllData()
    },
    add() {
      this.isAdd = true;
      const _this = this.$refs.form;
      _this.setMenus(this.data);
      _this.getMenuType();
      _this.form ={};
      _this.form = {
          appId: '',
          type: '1',
          orderNum: '1',
          icon: ''
      };
      _this.dialog = true;
      _this.form.appId = this.params.appId;
    },
    edit(data) {
      this.isAdd = false
      const _this = this.$refs.form
      _this.setMenus(this.data);
      _this.getMenuType();
      _this.form = data;
      //type为string类型的需要转一下
      _this.form.type=data.type.toString()
      _this.dialog = true
    },
    getAppList(){
      listAll().then(res => {
        if (res.code == Config.code_200) {
          this.appLists=res.data
        } else {
          this.$notify.error({ title: '失败', message: res.msg })
        }
      }).catch(err => {
        console.log(err.response.data.message)
      })
    },
    subDelete(id) {
      this.delLoading = true
      del(id).then(res => {
        if (res.code == Config.code_200) {
          this.initAllData()
          this.$notify({title: '删除成功',type: 'success',})
        } else {
          this.$notify.error({ title: '失败', message: res.msg })
        }
        this.delLoading = false
        this.$refs[id].doClose()
      }).catch(err => {
        this.delLoading = false
        this.$refs[id].doClose()
        console.log(err.response.data.message)
      })
    },
  }
}
</script>
<style scoped>
  .table_div {
    padding: 8px;
    border: 1px solid #ebeef5;
    background-color: #fff;
    color: #303133;
    -webkit-transition: 0.3s;
    transition: 0.3s;
  }

</style >

